package com.atguigu.async;

import java.util.concurrent.CompletableFuture;
import java.util.function.BiConsumer;
import java.util.function.Function;

public class FutureDemo03 {
    public static void main(String[] args) throws Exception {
        runAsync1();
        runAsync2();
        System.out.println("main线程在执行");
    }

    //发起一个异步请求

    /**
     * 你好runAsync1
     * main获取执行完异步方法之后的值:null
     * 你好runAsync2
     * main线程在执行
     * ForkJoinPool.commonPool-worker-9获取执行完异步方法之后的值:null
     */
    public static void runAsync1() {
        CompletableFuture.runAsync(new Runnable() {
            @Override
            public void run() {
                System.out.println("你好runAsync1");
            }
        }).whenComplete(new BiConsumer<Void, Throwable>() {
            @Override
            public void accept(Void acceptVal, Throwable throwable) {
                System.out.println(Thread.currentThread().getName() + "获取执行完异步方法之后的值:" + acceptVal);
            }
        });
    }

    public static void runAsync2() {
        CompletableFuture.runAsync(new Runnable() {
            @Override
            public void run() {
                System.out.println("你好runAsync2");
            }
        }).whenCompleteAsync(new BiConsumer<Void, Throwable>() {
            @Override
            public void accept(Void acceptVal, Throwable throwable) {
                System.out.println(Thread.currentThread().getName() + "获取执行完异步方法之后的值:" + acceptVal);
            }
        });
    }

}
